package com.letv.app.ljodfh.ui.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.text.TextUtils;

import com.letv.app.ljodfh.base.BaseActivity;
import com.letv.app.ljodfh.base.Constances;
import com.letv.app.ljodfh.beans.NewsItem;
import com.letv.app.ljodfh.ui.activities.H5Activity;
import com.letv.app.ljodfh.ui.activities.LivingActivity;
import com.letv.app.ljodfh.ui.activities.NewesDetailActivity;
import com.letv.app.ljodfh.ui.activities.TopicDetailActivity;
import com.letv.app.ljodfh.ui.activities.VideoPlayActivity;
import com.letv.app.ljodfh.ui.adapter.HomePagerAdapter;

/**
 * Created by liuyu on 2017/8/21.
 */

public class SkipUtil {

    public static final int REQUEST_CODE_PLAY_BASE = 121;
    public static final String EX_POS = VideoPlayActivity.EXTRA_POS;
    public static final String EX_POS_ITEM = VideoPlayActivity.EXTRA_POS_ITEM;

    public static boolean skipWithNewsItem(@NonNull Context context, NewsItem item,String category,String specialId) {
        if (item == null || !item.isNewsTypeValued()) {
            return false;
        }
        Intent intent = null;
        switch (item.getNewsType()) {
            case Constances.NEWS_TYPE_GRAPHIC:
            case Constances.NEWS_TYPE_AUDIO:
            case Constances.NEWS_TYPE_VIDEO:
                intent = new Intent(context, NewesDetailActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                break;
            case Constances.NEWS_TYPE_TOPIC:
                intent = new Intent(context, TopicDetailActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                break;
            case Constances.NEWS_TYPE_LIVE:
                intent = new Intent(context, LivingActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                intent.putExtra(BaseActivity.EX_DATA, item.getNewsImage());
                break;
            case Constances.NEWS_TYPE_H5:
                intent = new Intent(context, H5Activity.class);
                intent.putExtra(H5Activity.NEWS_ID, item.getNewsId());
                intent.putExtra(H5Activity.EX_HTML, item.getNewsUrl());
                break;
        }
        if(!TextUtils.isEmpty(specialId)){
            intent.putExtra(NewesDetailActivity.SPECIALID, specialId);
        }
        if (intent != null) {
            intent.putExtra(NewesDetailActivity.CATEGORY, category);
            context.startActivity(intent);
            return true;
        }
        return false;
    }
    public static boolean skipWithNewsItem(@NonNull Context context, NewsItem item,String category) {
        if (item == null || !item.isNewsTypeValued()) {
            return false;
        }
        Intent intent = null;
        switch (item.getNewsType()) {
            case Constances.NEWS_TYPE_GRAPHIC:
            case Constances.NEWS_TYPE_AUDIO:
            case Constances.NEWS_TYPE_VIDEO:
                intent = new Intent(context, NewesDetailActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                break;
            case Constances.NEWS_TYPE_TOPIC:
                intent = new Intent(context, TopicDetailActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                break;
            case Constances.NEWS_TYPE_LIVE:
                intent = new Intent(context, LivingActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                intent.putExtra(BaseActivity.EX_DATA, item.getNewsImage());
                break;
            case Constances.NEWS_TYPE_H5:
                intent = new Intent(context, H5Activity.class);
                intent.putExtra(H5Activity.NEWS_ID, item.getNewsId());
                intent.putExtra(H5Activity.EX_HTML, item.getNewsUrl());
                break;
        }

        if (intent != null) {
            intent.putExtra(NewesDetailActivity.CATEGORY, category);
            context.startActivity(intent);
            return true;
        }
        return false;
    }
    public static Intent getIntentWithNewsItem(@NonNull Context context, NewsItem item) {
        if (item == null || !item.isNewsTypeValued()) {
            return null;
        }
        Intent intent = null;
        switch (item.getNewsType()) {
            case Constances.NEWS_TYPE_GRAPHIC:
            case Constances.NEWS_TYPE_AUDIO:
            case Constances.NEWS_TYPE_VIDEO:
                intent = new Intent(context, NewesDetailActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                break;
            case Constances.NEWS_TYPE_TOPIC:
                intent = new Intent(context, TopicDetailActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                break;
            case Constances.NEWS_TYPE_LIVE:
                intent = new Intent(context, LivingActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                intent.putExtra(BaseActivity.EX_DATA, item.getNewsImage());
                break;
            case Constances.NEWS_TYPE_H5:
                intent = new Intent(context, H5Activity.class);
                intent.putExtra(H5Activity.NEWS_ID, item.getNewsId());
                intent.putExtra(H5Activity.EX_HTML, item.getNewsUrl());
                break;
        }
        if (intent != null) {
//			context.startActivity(intent);
            return intent;
        }
        return null;
    }

    public static void skipToVideoPlay(Activity ac, String url, int playedPos, int itemPos) {
//        ac.startActivityForResult();
        if (ac==null || TextUtils.isEmpty(url)){
            return;
        }
        Intent intent = new Intent(ac, VideoPlayActivity.class);
        intent.putExtra(VideoPlayActivity.EX_URL, url);
        intent.putExtra(VideoPlayActivity.EXTRA_POS, playedPos);
        intent.putExtra(VideoPlayActivity.EXTRA_POS_ITEM, itemPos);
        ac.startActivityForResult(intent, REQUEST_CODE_PLAY_BASE);
    }

    public static void skipToVideoPlay(Fragment fg, String url, int playedPos, int itemPos) {
//        ac.startActivityForResult();
        if (fg==null || TextUtils.isEmpty(url)){
            return;
        }
        Intent intent = new Intent(fg.getContext(), VideoPlayActivity.class);
        intent.putExtra(VideoPlayActivity.EX_URL, url);
        intent.putExtra(VideoPlayActivity.EXTRA_POS, playedPos);
        intent.putExtra(VideoPlayActivity.EXTRA_POS_ITEM, itemPos);
        fg.startActivityForResult(intent, REQUEST_CODE_PLAY_BASE);
    }
}
